package com.paireach.bw.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paireach.bw.R;
import com.paireach.bw.activity.AboutUsActivity;
import com.paireach.bw.activity.ChangePasswordActivity;
import com.paireach.bw.activity.CommonQuestionActivity;
import com.paireach.bw.activity.DriverExceptionPostActivity;
import com.paireach.bw.activity.FeedBackActivity;
import com.paireach.bw.activity.LoginActivity;
import com.paireach.bw.activity.ModifyPersonalActivity;
import com.paireach.bw.activity.TraceActivity;
import com.paireach.bw.activity.VersionInfoActivity;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseFragment;
import com.paireach.bw.common.Constant;
import com.paireach.bw.utils.BDTraceUtil;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by ${cqc} on 2017/2/3.
 */

public class PersonalFrag extends BaseFragment implements View.OnClickListener {

    private static final String TAG = "PersonalCenterFragment";
    private View view;
    private Button logout;
    private RelativeLayout rl_exception_commit;
    private RelativeLayout rl_personal_option;
    private RelativeLayout rl_changepsw;
    private RelativeLayout rl_versioninfo;
    private RelativeLayout rl_aboutus;
    private RelativeLayout rl_feedback;
    private RelativeLayout rl_common_question;
    private ImageView layout_top_back;
    private TextView layout_top_change;
    private Button btn_trace;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frag_personal, null);
        findViews();
        initViews();
        return view;
    }

    private void findViews() {
        layout_top_back = (ImageView) view.findViewById(R.id.layout_top_back);
        layout_top_change = (TextView) view.findViewById(R.id.layout_top_change);

        rl_exception_commit = (RelativeLayout) view.findViewById(R.id.rl_exception_commit);
        rl_personal_option = (RelativeLayout) view.findViewById(R.id.rl_personal_option);
        rl_changepsw = (RelativeLayout) view.findViewById(R.id.rl_changepsw);
        rl_versioninfo = (RelativeLayout) view.findViewById(R.id.rl_versioninfo);
        rl_aboutus = (RelativeLayout) view.findViewById(R.id.rl_aboutus);
        rl_feedback = (RelativeLayout) view.findViewById(R.id.rl_feedback);
        rl_common_question = (RelativeLayout) view.findViewById(R.id.rl_common_question);
        logout = (Button) view.findViewById(R.id.bt_logout);

        btn_trace = (Button) view.findViewById(R.id.btn_trace);
    }

    private void initViews() {
        layout_top_back.setVisibility(View.GONE);
        layout_top_change.setText("个人中心");
        rl_exception_commit.setOnClickListener(this);
        rl_personal_option.setOnClickListener(this);
        rl_changepsw.setOnClickListener(this);
        rl_versioninfo.setOnClickListener(this);
        rl_aboutus.setOnClickListener(this);
        rl_feedback.setOnClickListener(this);
        rl_common_question.setOnClickListener(this);
        logout.setOnClickListener(this);

        btn_trace.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_logout:
                AlertDialog.Builder builder = new AlertDialog.Builder(context)
                        .setTitle("是否退出登录?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                MyApplication.getSpUtils().putString(Constant.LOGIN_NAME, "");
                                MyApplication.getSpUtils().putString(Constant.TEL, "");
                                MyApplication.getSpUtils().putString(Constant.PWD, "");
                                MyApplication.getSpUtils().putString(Constant.REAL_NAME, "");
                                MyApplication.getSpUtils().putString(Constant.DRIVER_NUM, "");

                                JPushInterface.setAlias(context, "", new TagAliasCallback() {
                                    @Override
                                    public void gotResult(int code, String s, Set<String> set) {
                                        switch (code) {
                                            case 0:
                                                //showToast("s=" + s + ",code=" + code + "设置别名成功");
                                                break;
                                            case 6002:
                                                //showToast("s=" + s + ",code=" + code + "设置别名失败");
                                                break;
                                            default:
                                                //showToast("s=" + s + ",code=" + code);
                                                break;
                                        }
                                    }
                                });

                                BDTraceUtil.closeTrace();
                                ActivityOptionsCompat options1 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
                                ActivityCompat.startActivity(getActivity(), new Intent(getActivity(), LoginActivity.class), options1.toBundle());
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        getActivity().finish();
                                    }
                                }, 500);
                            }
                        })
                        .setNegativeButton("取消", null)
                        .setCancelable(true);

                AlertDialog dialog = builder.create();
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();
                break;
            case R.id.rl_exception_commit:
                ActivityOptionsCompat options8 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
                ActivityCompat.startActivity(getActivity(), new Intent(getActivity(), DriverExceptionPostActivity.class), options8.toBundle());
                break;
            case R.id.rl_personal_option:
                ActivityOptionsCompat options7 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
                ActivityCompat.startActivity(getActivity(), new Intent(getActivity(), ModifyPersonalActivity.class), options7.toBundle());
                break;
            case R.id.rl_changepsw:
                ActivityOptionsCompat options2 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
                ActivityCompat.startActivity(getActivity(), new Intent(getActivity(), ChangePasswordActivity.class), options2.toBundle());
                break;
            case R.id.rl_versioninfo:
                ActivityOptionsCompat options3 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
                ActivityCompat.startActivity(getActivity(), new Intent(getActivity(), VersionInfoActivity.class), options3.toBundle());
                break;
            case R.id.rl_aboutus:
                ActivityOptionsCompat options4 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
                ActivityCompat.startActivity(getActivity(), new Intent(getActivity(), AboutUsActivity.class), options4.toBundle());
                break;
            case R.id.rl_feedback:
                ActivityOptionsCompat options5 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
                ActivityCompat.startActivity(getActivity(), new Intent(getActivity(), FeedBackActivity.class), options5.toBundle());
                break;
            case R.id.rl_common_question:
//                ActivityOptionsCompat options6 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
//                ActivityCompat.startActivity(getActivity(), new Intent(getActivity(), CommonQuestionActivity.class), options6.toBundle());

                startActivity(new Intent(getActivity(), CommonQuestionActivity.class));
                getActivity().overridePendingTransition(R.anim.right_to_current, R.anim.current_to_left);
                break;
            case R.id.btn_trace:
                ActivityOptionsCompat options9 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
                ActivityCompat.startActivity(getActivity(), new Intent(getActivity(), TraceActivity.class), options9.toBundle());
                break;
        }
    }
}
